﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="old.aspx.cs" Inherits="OpenRAMS.Maintenance.oldWorkTickets" %>
<%@ Import Namespace="OpenRAMS.Model" %>

<script runat="server">
    void backNow_Click(Object sender, EventArgs e)
    {
        Response.Redirect("Maintenance.aspx");
    }      
      
</script>

<!DOCTYPE html />

<html>
<head runat="server">
    <title>OpenRAMS - Work Ticket Archive</title>
    <link rel="Stylesheet" type="text/css" href="../Include/default.css" />
    <meta http-equiv="refresh" content="180" />
</head>
<body>
    <form id="form1" runat="server">
    <asp:Button ID="backNow" Text="<--- Back" OnClick="backNow_Click" runat="server" Width="15%"/>
    <table width="100%" class="tableDefault">
    <tr>
    <td align="center"><div class="tableTitle">Active Work Tickets</div></td>
    </tr>
    <tr>
    <td><div class="tableBody">
        <table width="100%">
        <tr><td>
        <%
            WorkTickets wtList = new WorkTickets();
            wtList.Populate("wontExist","bitActive='false'");
            List<WorkTicket> wts = wtList.getWorkTicketList();

            if (wts.Count() > 1)
            {
                //arrange bookings so most recently created ticket appears first
                wts.Sort(delegate(WorkTicket a, WorkTicket b)
                {
                    int x = a.createdTimeStamp.CompareTo(b.createdTimeStamp);
                    if (x != 0) return -x;  //just make this positive to reverse the order ;)
                    else return a.createdTimeStamp.CompareTo(b.createdTimeStamp);
                });
                
                int loopCount = 0;

                Response.Write("<tr class=\"listTop\"><td><b>ID</b></td><td><b>Title</b></td><td><b>Description</b></td><td><b>Location</b></td><td><b>Allocated to</b></td><td><b>Requester</b></td><td><b>Status</b></td><td><b>Created Date</b></td><td><b>Approve?</b></td></tr>");
                Response.Write("<tr><td colspan=\"10\"><hr /></td></tr>");

                foreach (WorkTicket ticket in wts)
                {

                    Boolean result = false; //used later to set row colour & define clash link
                    int clashID = -1;

                   
                    Response.Write("<td>");
                    Response.Write(ticket.workTicketID + "</td>");
                    Response.Write("<td>");
                    Response.Write(ticket.workTicketTitle);
                    Response.Write("</td><td>");
                    Response.Write(ticket.workTicketDesc);
                    Response.Write("</td><td>");
                    
                    //grab location
                    Response.Write(ticket.workTicketLocation);

                    Response.Write("</td><td>");

                    
                    
                    UserRecord niceCreatorName = new UserRecord();
                    niceCreatorName.Load(ticket.workTicketAllocatedTo);
                    Response.Write("<a href=\"mailto:" + niceCreatorName.userEmail + "\">");
                    Response.Write(niceCreatorName.userFirstName + " " + niceCreatorName.userSecondName);
                    Response.Write("</a>");

                    Response.Write("</td><td>");
                    niceCreatorName.Load(ticket.workTicketCreator);
                    
                    Response.Write("<a href=\"mailto:" + niceCreatorName.userEmail + "\">");
                    Response.Write(niceCreatorName.userFirstName + " " + niceCreatorName.userSecondName);
                    Response.Write("</a>");
                    
                    Response.Write("</td><td>");
                    
                    //to get current status, find most recent work log
                    WorkTicketLogs listLogs = new WorkTicketLogs();
                    listLogs.Populate(ticket.workTicketID);
                    List<WorkTicketLog> logs = listLogs.getWorkTicketLogList();

                    //arrange so most recently created ticket appears first
                    logs.Sort(delegate(WorkTicketLog a, WorkTicketLog b)
                    {
                        int x = a.workTicketLogTime.CompareTo(b.workTicketLogTime);
                        if (x != 0) return -x;  //just make this positive to resource the order ;)
                        else return a.workTicketLogTime.CompareTo(b.workTicketLogTime);
                    });

                    try
                    {
                        WorkTicketLog recentLog = logs.First<WorkTicketLog>();
                        Response.Write(recentLog.workTicketLogCategory);
                    }
                    catch
                    {
                        Response.Write("N/A");  //this should only occur if you're running test cases & haven't written work logs when creating things!
                    }
                    
                    Response.Write("</td><td>");
                    
                    Response.Write(ticket.createdTimeStamp.ToString("ddd d MMM yyyy HH:mm"));
                    Response.Write("</td><td>");


                        Response.Write("<a href=\"edit.aspx?wtID=" + ticket.workTicketID + "\">Edit</a> ");
                                         
                    Response.Write("<a href=\"finish.aspx?wtID=" + ticket.workTicketID + "\">Finish</a>");
                    Response.Write("</td></tr>");
                    if(loopCount < wts.Count() - 1)
                    {
                        Response.Write("<tr><td colspan=\"10\"><hr /></td></tr>");
                    }
                    loopCount++;
                }                
                
            }
            else
            {
                Response.Write("<div align=\"center\"><i>There are no active bookings.</i></div>");
            }
            
         %>        
        </td></tr>
        </table>
    </div>
    
    
    </td>
    </tr>

    </table>
    </form>
</body>
</html>
